//
//  AppDelegate.m
//  QXShowing
//
//  Created by 王道道 on 2018/4/11.
//  Copyright © 2018年 doll_dream. All rights reserved.
//

#import "AppDelegate.h"

#import "UIViewController+BHMediaExt.h"

#import "BaseTabBarController.h"
#import "NewLoginViewController.h"

#import "ContactDataObject.h"
#import "AppDelegate+EaseMob.h"

//#define BaiduSDK  @"Vge1l77E6mmCINptyal8o4ZaFTth0V5Y"
#define BaiduSDK  @"7moeD9axMVdIhQFjwlKks7lzYanKiQ9H"

@interface AppDelegate ()<BMKGeneralDelegate>

@property (nonatomic, strong)BMKMapManager *mapManager;

@property (nonatomic, strong) BaseTabBarController *tabBarController;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
#if defined(DEBUG) || defined(_DEBUG)
//    [[FHHFPSIndicator sharedFPSIndicator] show];
#endif
    
    //    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    //百度地图初始化
//    [self initBaiduMap];
    //要使用百度地图，请先启动BaiduMapManager
    _mapManager = [[BMKMapManager alloc]init];
    BOOL ret = [self.mapManager start:BaiduSDK generalDelegate:self];
    if (!ret) {
        NSLog(@"manager start failed");
    }
    //分享
//    [self shareSdkAction];
    
    //    //向微信注册**********（商户APPID） 微信支付
//    [WXApi registerApp:@"wxc4707aed43e3fe51"];
    
    //连接数据库
//    [[ContactDataObject shareInstance] createDateBase];
    
    //注册mob
    NSString *apnsCertName = nil;
    apnsCertName = @"zhaunhui";
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    NSString *appkey = [ud stringForKey:@"identifier_appkey"];
    if (!appkey) {
        appkey = @"1158180523146355#zhaunhui";
        [ud setObject:appkey forKey:@"identifier_appkey"];
    }
    EMOptions *options = [EMOptions optionsWithAppkey:appkey];
    options.apnsCertName = apnsCertName;
    [[EMClient sharedClient] initializeSDKWithOptions:options];
    
    
    [self gotoMian];
    
    
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (void)gotoMian {
    self.tabBarController = [[BaseTabBarController alloc] init];
    //    PublicWelfareViewController *publicWelfareVC = [[PublicWelfareViewController alloc]init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.tabBarController];
    [nav setNavigationBarHidden:YES];
//    self.window.backgroundColor = Main_BackColor;
    self.window.rootViewController = nav;
}

- (void)gotoLogin:(UIViewController *)vc {
    NewLoginViewController *loginVC = [[NewLoginViewController alloc]init];
    loginVC.hidesBottomBarWhenPushed = YES;
    if (vc) {
        loginVC.isLogonFailure = NO;
        [vc.navigationController pushViewController:loginVC animated:YES];
    }else {
        UIViewController *topmostVC = [UIViewController topViewController];
        if (![topmostVC isKindOfClass:[NewLoginViewController class]]) {
            loginVC.isLogonFailure = YES;
            [topmostVC.navigationController pushViewController:loginVC animated:YES];
        }else {
            NSLog(@"11312312312%@",NSStringFromClass([topmostVC class]));
        }
    }
}

- (void)toggleVC:(NSInteger)selectedIndex {
    self.tabBarController.selectedIndex = selectedIndex;
}

//统计为读消息条数
- (void)setupUnreadMessageCount {
    NSArray *conversations = [[EMClient sharedClient].chatManager getAllConversations];
    NSInteger unreadCount = 0;
    for (EMConversation *conversation in conversations) {
        unreadCount += conversation.unreadMessagesCount;
    }
    
    UITabBarItem *item = self.tabBarController.tabBar.items[1];
    if (unreadCount == 0) {
        item.badgeValue = nil;
    }else {
        if (unreadCount > 99) {
            item.badgeValue = @"99+";
        }else {
            item.badgeValue = [NSString stringWithFormat:@"%ld",unreadCount];
        }
    }
}

@end
